const { response } = require('express')
const mysql = require('mysql')
const Message = require('./message')

const pool = mysql.createPool({
	host: 'localhost',
	user: 'root',
	password: '123456',
	port: '3306',
	database: 'lang_first'
})
// 封装一个数据库模型基类
module.exports = class Model {
	static query(sql, params) {
		return new Promise((resolve, reject) => {
			pool.getConnection(function (err, connection){
				if(err) {
					console.log(err)
					connection.release()
				}else{
					// query执行sql语句
					connection.query(sql,params,(err,result)=>{
						if(err){
							console.log(err);
							reject(err)
						}else{
							resolve(result)
						}
						// 结束会话,释放连接
						connection.release()
					})
				}
			})
		})
	}

	static formatParams(){
		let arr = []
		for(let i=0,l = arguments.length;i<l;i++){
			arr.push(this.arguments[i])
		}
		return arr
	}
}
